﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GameListCreator.ViewModels;
using System.Diagnostics;
using Microsoft.Win32;

using GameListCreator.Extenders;

namespace GameListCreator.Controls
{
    /// <summary>
    /// Interaction logic for GameEditor.xaml
    /// </summary>
    public partial class GameEditor : UserControl
    {
        /// <summary>
        /// Constructeur
        /// </summary>

        public GameEditor()
        {
            GameEditor.Instance = this;

            InitializeComponent();

            this.LayoutRoot.DataContext = App.GameListViewModel;

            App.GameListViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(GameListViewModel_PropertyChanged);
        
        }

        /// <summary>
        /// Changement de propriété
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void GameListViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case GameListViewModel.SelectedGamePropertyName :
                    // Remise à zero
                    this.TextBoxVacuum.Text = null;
                    break;
            }
        }

        /// <summary>
        /// Instance
        /// </summary>

        public static GameEditor Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Aspiration
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonVacuum_Click(object sender, RoutedEventArgs e)
        {
            if (App.GameListViewModel.SelectedGame != null)
            {
                App.GameListViewModel.SelectedGame.VacuumGbDbGameUri(this.TextBoxVacuum.Text);
            }
        }

        /// <summary>
        /// On Drop quelquechose
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void LayoutRoot_Drop(object sender, DragEventArgs e)
        {
            this.DropData(e, App.GameListViewModel.SelectedGame );
        }

        /// <summary>
        /// Drop quelquechose
        /// </summary>
        /// <param name="e"></param>

        public bool DropData(DragEventArgs e, GameViewModel selectedGame )
        {
            if (selectedGame != null)
            {
                try
                {
                    VacuumResult result = selectedGame.Vacuum(e.Data );

                    switch (result.VacuumData)
                    {
                        case VacuumDataTypes.GbDbUri:
                            this.TextBoxVacuum.Text = result.Data;
                            break;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }

                return true;
            }

            return false;
        }

        /// <summary>
        /// Ouverture de la page OpenGbDb
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void OpenGbDbPage_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("http://www.gbdb.org/games_list.php");
        }

        /// <summary>
        /// Ouverture de la Rom
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonRomOpen_Click(object sender, RoutedEventArgs e)
        {
            if (App.GameListViewModel.SelectedGame != null)
            {
                OpenFileDialog openDialog = new OpenFileDialog();

                openDialog.DefaultExt = "*.gb|Rom GameBoy";

                if (openDialog.ShowDialog(this.GetWindow()) == true)
                {
                    string filename = openDialog.FileName;

                    App.GameListViewModel.SelectedGame.VacuumRom(filename);
                }
            }
        }

        /// <summary>
        /// Destruction de la Rom
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonRomDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this Rom ?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                App.GameListViewModel.SelectedGame.DeleteRom();
            }
        }

        /// <summary>
        /// Ajouter une série
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonAddSerie_Click(object sender, RoutedEventArgs e)
        {
            if (App.GameListViewModel.AddSerie(this.ComboBoxSerie.Text) == true)
            {
                this.ComboBoxSerie.SelectedItem = this.ComboBoxSerie.Text;
                MessageBox.Show("Serie added !", "", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        /// <summary>
        /// Retirer une série
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ButtonRemoveSerie_Click(object sender, RoutedEventArgs e)
        {
            App.GameListViewModel.RemoveSerie(this.ComboBoxSerie.Text);
            this.ComboBoxSerie.SelectedItem = null;
        }
    }
}
